/**
 * @author ling007
 * @date 2024/7/1 10:10
 * @desciption:
 */
public class VolatileExample {

    private volatile boolean flag = false;

    public void write(){
        flag = true;
    }

    public void reader() {
        while (!flag) {
            // 等待flag变为true
        }
        System.out.println("Flag is now true.");
    }

    public static void main(String[] args) {
        VolatileExample example = new VolatileExample();

        Thread writeThread = new Thread(() -> {
            example.write();
        });

        Thread readThread = new Thread(() -> {
            example.reader();
        });

        writeThread.start();
        readThread.start();
    }
}
